package com.example.demo.concurrent;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： 76875
 * @date： 2022/1/6 星期四 19:41
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
public class MyThreadPool {

    private static volatile ThreadPoolExecutor threadPoolExecutor;

    public static ThreadPoolExecutor getThreadPoolExecutor(){
        if (threadPoolExecutor == null){
            synchronized (MyThreadPool.class){
                if (threadPoolExecutor == null){
                    BasicThreadFactory.Builder builder = new BasicThreadFactory.Builder();
                    builder.namingPattern("MyThreadPool-%d");
                    BasicThreadFactory basicThreadFactory = builder.build();
                    threadPoolExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
                            Runtime.getRuntime().availableProcessors(),
                            100,
                            TimeUnit.MINUTES,
                            new LinkedBlockingDeque<>(),
                            basicThreadFactory);
                }
            }
        }
        return threadPoolExecutor;
    }
}
